package com.xiaok.mobileredis.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xiaok.mobileredis.request.GetPacketReq;
import com.xiaok.mobileredis.request.SaveRedPacketReq;
import com.xiaok.mobileredis.entity.RedPacketInfo;
import com.xiaok.mobileredis.entity.RedPacketRecord;
import com.xiaok.mobileredis.service.RedPacketInfoService;
import com.xiaok.mobileredis.service.RedPacketRecordService;
import com.xiaok.mobileredis.utils.RedisService;
import com.xiaok.mobileredis.utils.JsonData;
import com.xiaok.mobileredis.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @ClassName：RedPacketInfoController
 * @Description：抢红包
 * @Author：chenyb
 * @Date：2020/8/24 10:39 下午
 * @Versiion：1.0
 */
@RestController
@RequestMapping("red_packet_info")
public class RedPacketInfoController {
    @Resource
    private RedisService redisService;
    @Resource
    private RedPacketInfoService redPacketInfoService;
    @Resource
    private RedPacketRecordService redPacketRecordService;

    @Autowired
    private RedisUtils redisUtils;
    private static final String TOTAL_NUM = "_totalNum";
    private static final String TOTAL_AMOUNT = "_totalAmount";

    /**
     * 发红包
     *
     * @param req 用户信息
     * @return
     */
    @PostMapping("addPacket")
    public JsonData saveRedPacket(@RequestBody SaveRedPacketReq req) {
        RedPacketInfo redPacketInfo = new RedPacketInfo();
        redPacketInfo.setUid(req.getUid());
        redPacketInfo.setTotalAmount(req.getTotalAmount());
        redPacketInfo.setTotalPacket(req.getTotalNum());
        redPacketInfo.setRemainingAmount(req.getTotalAmount());
        redPacketInfo.setRemainingPacket(req.getTotalNum());
        redPacketInfo.setCreateTime(new Date());
        //分布式情况下，最好使用雪花算法生成
        Long redPacketId = System.currentTimeMillis();
        redPacketInfo.setRedPacketId(redPacketId);
        redPacketInfoService.insert(redPacketInfo);
        //往redis插入2条记录
        //红包个数
        redisService.set(redPacketId + TOTAL_NUM, req.getTotalNum() + "");
        //红包金额
        redisService.set(redPacketId + TOTAL_AMOUNT, req.getTotalAmount() + "");
        return JsonData.buildSuccess();
    }

    /**
     * 抢红包
     *
     * @param req
     * @return
     */
    @PostMapping("getPacket")
    public JsonData getRedPacket(@RequestBody GetPacketReq req) {
        Integer randomAmount=0;
        String redPacketName = req.getRedPacketId() + TOTAL_NUM;
        //红包总金额
        String totalAmountName = req.getRedPacketId() + TOTAL_AMOUNT;
        if (redisService.exists(redPacketName)) {
            Integer num = Integer.valueOf(redisService.get(redPacketName) + "");
            if (num != null && num > 0) {
                //红包数减一
                redisService.decr(redPacketName);

                //拿到红包总金额
                if (redisService.exists(totalAmountName)){
                    Integer totalAmount=Integer.valueOf(redisService.get(totalAmountName)+"");
                    //红包最大金额
                    Integer maxMoney=totalAmount/Integer.valueOf(num)*2;
                    Random random=new Random();
                    //随机抢到的红包
                    randomAmount =num==1?totalAmount:random.nextInt(maxMoney);
                    //redis红包金额减指定值
                    redisService.decr(totalAmountName,randomAmount);
                }
                updateRedPacketInDB(req.getUid(),req.getRedPacketId(),randomAmount);
                return JsonData.buildSuccess("抢到红包:"+randomAmount);
            } else {
                return JsonData.buildError("红包被抢完啦");
            }
        } else {
            return JsonData.buildError("红包不存在，请重试");
        }
    }

    /**
     * 更新用户抢到红包记录
     * @param uid 用户id
     * @param redPacketId 红包id
     * @param amount 金额
     */
    private void updateRedPacketInDB(int uid,Long redPacketId,int amount){
        RedPacketRecord redPacketRecord=new RedPacketRecord();
        redPacketRecord.setUid(uid);
        redPacketRecord.setNickName("陈彦斌");
        redPacketRecord.setImgUrl("https://images.cnblogs.com/cnblogs_com/chenyanbin/1560326/o_qianxun.jpg");
        redPacketRecord.setRedPacketId(redPacketId);
        redPacketRecord.setAmount(amount);
        redPacketRecord.setCreateTime(new Date());
        redPacketRecordService.insert(redPacketRecord);
        //这里还需要更新发红包的数据，红包总个数-1；总金额减去已抢红包数，这里不实现了，自行实现功能
    }

    /**
     * 测试整合
     * @return
     */
    @GetMapping("/test")
    public Object test(){
        String strJson = (String) redisUtils.get("redPacketRecordCache");
        if (strJson==null){
            System.out.println("从db取值");
            // step2如果拿不到则从DB取值
            List<RedPacketRecord> redPacketRecords =  redPacketRecordService.selectList(null);
            // step3 DB非空情况刷新redis值
            if (redPacketRecords!=null){
                redisUtils.set("redPacketRecordCache", JSON.toJSONString(redPacketRecords));
                return redPacketRecords;
            }
            return null;
        }else
        {
            System.out.println("从redis缓存取值");
            return JSONObject.parseArray(strJson,RedPacketRecord.class);
        }
    }
}
